package com.berwin.caper.view.components.filetree;

import javax.swing.*;
import java.awt.*;

public class JFileTreePopMenu extends JPopupMenu {

    private boolean localMenu;
    private String currentPath;

    public JFileTreePopMenu(boolean localMenu) {
        this.localMenu = localMenu;
        if (this.localMenu)
            this.initLocalMenu();
        else
            this.initRemoteMenu();
    }

    private void initLocalMenu() {
        JMenuItem itemUpload = new JMenuItem("上传");
        this.add(itemUpload);
        this.addSeparator();

        JMenuItem itemOpen = new JMenuItem("打开");
        this.add(itemOpen);
        JMenuItem itemCopyURL = new JMenuItem("拷贝URL");
        this.add(itemCopyURL);
        this.addSeparator();

        JMenuItem itemDelete = new JMenuItem("删除");
        this.add(itemDelete);
    }

    private void initRemoteMenu() {
        JMenuItem itemDownload = new JMenuItem("下载");
        this.add(itemDownload);
        this.addSeparator();

        JMenuItem itemOpen = new JMenuItem("打开");
        this.add(itemOpen);
        JMenuItem itemCopyURL = new JMenuItem("拷贝URL");
        this.add(itemCopyURL);
        this.addSeparator();

        JMenuItem itemDelete = new JMenuItem("删除");
        this.add(itemDelete);

    }

    public void show(Component invoker, int x, int y, JFileNode curNode) {
        super.show(invoker, x, y);
        JMenuItem item = (JMenuItem) this.getComponent(0);
        item.setText((this.localMenu ? "上传" : "下载") + curNode.getName());
        this.repaint();
        this.currentPath = curNode.getFilePath();
    }
}
